<?php
// +----------------------------------------------------------------------
// | UBTEN.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------
namespace User\Controller;
use Think\Controller;

class LoginController extends Controller{
    
    public function _initialize() {
        $ob = M("config");
        $config = $ob->field('name,data')->select();
        foreach ($config as $key => $value) {
            C($value['name'], $value['data']);
        }
    }
    
    // 用户登录
    public function index(){
        $this->display();
    }
    
    public function mobile(){
        if(session('forward')){
            $forward = session('forward');
        }else if(I('forward')){
            $forward = I('forward');
        }else{
            $forward = U('Index/index');
        }
        $user_mobile = trim(I('post.user_mobile'));
        $user_code = I('post.user_code');
        $user_sms = I('post.user_sms');
        $session_sms = session('mobile_sms');
        //判断
        $re = $this->check_verify($user_code);
        if($re){
            if($user_sms == $session_sms){
                session('mobile_sms',null);
                $users = D('Users');
                $res = $users->login($user_mobile, 0,1);
                if($res){
                    //用户若设置自动登陆则session过期时间改为7天
                    $this->success('尊敬的'.session('user_name').' 欢迎您的到来!', $forward);
                }
            }else{
                $this->error('手机验证码不正确！');
            }
        }else{
            $this->error('验证码不正确！');
        }
    }
    
    public function email(){
        if(session('forward')){
            $forward = session('forward');
        }else if(I('forward')){
            $forward = I('forward');
        }else{
            $forward = U('Index/index');
        }
        $user_email = trim(I('post.user_email'));
        $user_code = I('post.user_code');
        $user_passwd = I('post.user_passwd');
        //判断
        $re = $this->check_verify($user_code);
        if($re){
            $users = D('Users');
            $res = $users->login($user_email, $user_passwd,1);
            if($res){
                //用户若设置自动登陆则session过期时间改为7天
                $this->success('尊敬的'.session('user_auth.username').' 欢迎您的到来!', $forward);
            }
        }else{
            $this->error('验证码不正确！');
        }
    }
    

    // 验证验证码
    public function check_verify($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }
    
    public function Out() {
        header("Access-Control-Allow-Origin: *");
        session('user_id', null);
        session('user_name', null);
        session('[destroy]');
        $a['status'] = 1;
        $a['msg'] = '退出成功!';
        $this->ajaxReturn($a);
    }
}